<?php //strip
/**
 * Helper class that is used when odWpPlugin.php is running from the console.
 * 
 * @package odWpPlugin
 * @author Ondrej Donek <ondrejd@gmail.com>
 */
class odWpPlugin_Cli
{
  /**
   * Holds arguments passed to the script.
   * @var array
   */
  protected $mArguments = null;
  
  /**
   * Holds count of arguments passed to the script.
   * 
   * @var integer
   */
  protected $mArgumentsCount = 0;
  
  /**
   * Holds description of a last error.
   * 
   * @var string
   */
  protected $mLastError = null;
  
  /**
   * Holds name of an action parsed from the given arguments.
   * 
   * @var string
   */
  protected $mAction = 'help';
  
  /**
   * Holds options parsed from the given arguments.
   * 
   * @var array
   */
  protected $mOptions = array();
  
  /**
   * Constructor. Parse given arguments and initialize class.
   * 
   * @global array $argv
   * @return void
   */
  public function __construct() 
  {
    // Get command line arguments
    global $argv;
    $this->mArguments = $argv;
    array_shift($this->mArguments);
    $this->mArgumentsCount = count($this->mArguments);
    
    // Check arguments
    if(!$this->validateArgs()) {
      $this->mLastError = 'Wrong arguments, run with --help for more info.' . PHP_EOL;
    }
    
    // Parse arguments
    if(!$this->parseArgs()) {
      $this->mLastError = 'Arguments were not recognized. Try to run this script with `--help` argument.' . PHP_EOL;
    }
    
    // Now is `odWpPlugin_Cli` prepared for execution ...
  }
  
  /**
   * Execute the choosen action.
   * 
   * @return void
   */
  public function execute() 
  {
    // If there was any error occured during the validating and parsing 
    // the command line arguments display it and exit.
    if(!is_null($this->mLastError)) {
      if(function_exists('wp_die')) {
        wp_die($this->mLastError);
      } else {
        die($this->mLastError);
      }
    } 
    
    // Execute requested action.
    switch($this->mAction) {
      case 'version':
        $this->printVersion();
        break;
      
      case 'help':
      default:
        $this->printUsage();
        break;
    }
  }
  
  /**
   * Validates arguments passed to the script.
   * 
   * @return boolean
   */
  protected function validateArgs()
  {
    if(!is_array($this->mArguments)) return false;
    if($this->mArgumentsCount == 0) return false;
    if($this->mArgumentsCount == 1 && $this->mArguments[0] != '--help') return false;
    
    return true;
  }
  
  /**
   * Parses arugments passed to the script and returns an error if occured.
   * 
   * @return boolean
   * @todo Implement this!
   */
  protected function parseArgs()
  {
    if($this->mArgumentsCount == 1) {
      if($this->mArguments[0] == '--help' || $this->mArguments[0] == '-H') {
        $this->mAction = 'help';
        return true;
      }
      if($this->mArguments[0] == '--version' || $this->mArguments[0] == '-V') {
        $this->mAction = 'version';
        return true;
      }
    }
    
    throw new Exception('Not implemented yet!');
    
    return true;
  }
  
  /**
   * Prints usage of this script.
   * 
   * @return void
   */
  protected function printUsage()
  {
    echo 'Code generator for WordPress plugins based on `odWpPlugin` project.' . PHP_EOL;
    echo PHP_EOL;
    echo 'Usage: php odWpPlugin.php --create-[WHAT] [OPTIONS]' . PHP_EOL;
    echo '   or: php odWpPlugin.php --create-plugin -i=my-plugin -n=MyPlugin -v=0.1' . PHP_EOL;
    echo 'Generates [WHAT] code with which [OPTIONS].' . PHP_EOL;
    echo PHP_EOL;
    echo 'List of all command line arguments:' . PHP_EOL;
    echo '  Actions:' . PHP_EOL;
    echo '   -C, --create-plugin    Creates new plugin class.' . PHP_EOL;
    echo '   -M, --create-model     Creates new database model class.' . PHP_EOL;
    echo '   -T, --create-table     Creates new administration table class.' . PHP_EOL;
    echo '   -F, --create-form      Creates new form class.' . PHP_EOL;
    echo '  Values:' . PHP_EOL;
    echo '   -i=[PLUGIN_ID]         Plugin\'s ID. Uses also for gettext textdomain.' . PHP_EOL;
    echo '   -n=[PLUGIN_NAME]       Full name of the plugin.' . PHP_EOL;
    echo '   -v=[PLUGIN_VERSION]    Version of the plugin.' . PHP_EOL;
    echo '  Others:' . PHP_EOL;
    echo '   -H, --help             Prints this help.' . PHP_EOL;
    echo '   -V, --version          Prints version of this script.' . PHP_EOL;
    echo PHP_EOL;
    echo 'For more details see [http://www.ondrejd.info/projects/wordpress-plugins/odWpPlugin].' . PHP_EOL;
  }
  
  /**
   * Prints version of this script.
   * 
   * @return void
   */
  protected function printVersion()
  {
    echo odWpPlugin_Abstract::VERSION . PHP_EOL;
  }
} // End of odWpPlugin_Cli

#{{{ --- When running from commandline execute script immediately
if(PHP_SAPI == 'cli' && empty($_SERVER['REMOTE_ADDR'])):

$odWpPlugin_Cli = new odWpPlugin_Cli();
$odWpPlugin_Cli->execute();

endif;
#}}} --- running from command line
